<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 *  Notice Model
 *
 * @package XGO CMS v3.0
 * @subpackage Notice
 * @link http://sunsoft.vn
 */

class BookingStatus_model extends MY_Model{

	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_list_booking';
                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
        public function timeLimit($params){
            $data = array(
                'timeLimit'     =>      $params['timeLimit']
            );
            $this->db->where('id', $params['id']);
            $this->db->update($this->table_name, $data); 
//            echo $this->db->last_query();exit;
        }

        public function getListBookingTypes(){
            return $this->booking_types;
        }
        
         public function selectDetailCustomers($params,$joinTbl = null){
            $this->db->select('bklist.*,med.payment_name');
            $this->db->from($this->table_name . ' AS bklist');
            $this->db->join('book_payment_method AS med','med.id=bklist.payment_method','join');
            switch ($joinTbl){
                case '2':
                    break;
                case '3':
                    $this->db->select('bk.bank_name,account_name,account_number,aff');
                    $this->db->join('book_bankinfo_customers AS bkc','bkc.customerID=bklist.customerID','join');
                    $this->db->join('book_bank AS bk','bk.id=bkc.bankid','join');
                    break;
                default :
                    break;
            }
            $this->db->where('bklist.id',$params);
            $query = $this->db->get();
//                echo $this->db->last_query();exit;
            return $query->result();
        }

	public function init_data($right){
		return array(
                                array(
						'name' => '',
						'label' => '<a style="cursor: pointer;" id="voucherClick">CT</a><input type="hidden" id="voucherAdd" value=""/>',
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => false
				),array(
						'name' => 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '8%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '3%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'airline',
						'label' => $this->lang->line('airline'),
						'width' => '2%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'ship_name_customer',
						'label' => $this->lang->line('ship_name_customer'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'ship',
						'label' => $this->lang->line('ship'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'payment_method',
						'label' => $this->lang->line('payment_method'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'ticketWorkers',
						'label' => $this->lang->line('ticketWorkers'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'net_fare',
						'label' => $this->lang->line('net_fare'),
						'width' => '3%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'sale_fare',
						'label' => $this->lang->line('sale_fare'),
						'width' => '3%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'payment_company',
						'label' => $this->lang->line('payment_company'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'createdTime',
						'label' => $this->lang->line('createdTime'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name'  => 'button',
						'width' => '8%',
						'sort'  => FALSE,
						'label'  => false,
						'searchoptions' => false
				)
		);
	}
	
	/*
	 * datatables data
	* return json
	*/
	public function json_data($controller, $right){
            
                if($this->session->userdata('h_bookingStatus')){
                    $arr_status = json_decode($this->session->userdata('h_bookingStatus'));
                }
            
		$this->datatables
		->select('c.id, c.timeLimit,c.voucher_id, c.customerID, c.booking_place_code, c.airline, c.booking, c.booking_customer_name, c.booking_child, c.ticket_workers,c.net_fare, c.sale_fare,c.payment_confirm,c.createdTime,method.payment_name,bc.name AS khachhang,usr.username AS booker')
		->from($this->table_name.' AS c')
                ->join('book_payment_method AS method','method.id = c.payment_method','LEFT')
                ->join('book_contacts AS bc','bc.customerID = c.customerID')
                ->join('users AS usr','usr.id = c.owner');

                if(!in_array($this->session->userdata['group_id'],array(1,79))){
                    $this->datatables->where('c.payment_confirm = 0');
                    $this->datatables->where('c.owner',$this->session->userdata['user_id']);
                }
                
                if(isset($arr_status) && $arr_status->h_searchCustomersPhone != ''){
                    $this->datatables->where('bc.phone LIKE "%'.$arr_status->h_searchCustomersPhone.'%"');
                }
                if(isset($arr_status) && $arr_status->h_booking_place_code != ''){
                    $this->datatables->where('c.booking_place_code ',$arr_status->h_booking_place_code);
                }
                if(isset($arr_status) && $arr_status->h_beginTime != ''){
                    $this->db->where('c.createdTime BETWEEN '.$arr_status->h_beginTime. ' AND '.$arr_status->h_endTime);
                }

		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData']    = array();
                
                $this->session->unset_userdata('h_bookingStatus');

		foreach($datatables['aaData'] as $item){
                        $booking            = json_decode($item['booking']);
                        $string             = '';
                        $i                  = 0;
                        while($i < count($booking)){
                            $string .= $booking[$i].'<br/>';
                            $i++;
                        }
                        if($item['booking_child']){
                            $child = '<br/>(Có em bé)';
                        } else {
                            $child = '';
                        }
                        if($item['timeLimit']=="" || $item['timeLimit']=="0000-00-00 00:00:00"){
                            $style = '';
                            $timeLimit = '';
                        } else {
                            $now            = date("Y-m-d H:i:s");
                            $timeLimit      = ($item['timeLimit']=="")?NULL:date("d-m-Y H:i:s",strtotime($item['timeLimit']));
                            if(strtotime($now) >= strtotime($timeLimit)){
                                $style = 'style="font-weight:bold;color:red;"';
                            }
                        }

                        if($item['voucher_id']){
                            $ticketWorkers = $this->bookingStatus_model->getNameTicker($item['voucher_id']);
                            $ticketWorkers = $ticketWorkers[0]->name;
                        } else {
                            $ticketWorkers = '';
                        }
                        
                        if($item['voucher_id'] != NULL){
                            $chungtu    =   "Đã tạo CT";
                        } else {
                            $chungtu    = "<input type='checkbox' value='".$item['id']."' onclick=get_Checked_Checkbox_By_Name('checkVoucher') name='checkVoucher' id='checkVoucher' />";
                        }
                        
			$ouput['aaData'][] = array(
                                        $chungtu,
					'<span '.$style.'>'.$item['khachhang'].'</span>',
                            		'<span '.$style.'>'.$item['booking_place_code'].'</span>',
                                        '<span '.$style.'>'.$item['airline'].'</span>',
                                        '<span '.$style.'>'.$this->bookinglib->getFirstCustomer($item['booking_customer_name']).$child.'</span>',
                                        '<span '.$style.'>'.$string.'</span>',
                                        '<span '.$style.'>'.$item['payment_name'].'</span>',
                                        '<span '.$style.'>'.$ticketWorkers.'</span>',
                                        '<span '.$style.'>'.$this->bookinglib->my_number_format($item['net_fare'],2, ',', ',').'</span>',
                                        '<span '.$style.'>'.$this->bookinglib->my_number_format($item['sale_fare'],2, ',', ',').'</span>',
                                        ($item['payment_confirm'] == 1)?'<span '.$style.'>'.'Đã thanh toán'.'</span>':'<span '.$style.'>'.'Chưa thanh toán'.'</span>',
                                        '<span '.$style.'>'.$item['booker'].'</span>',
                                        '<span '.$style.'>'.date("d-m-Y H:i:s",$item['createdTime']).'</span>',
					$this->add_button_status($controller, $right, $item['id'])
			);
			unset($chungtu);
		}

		return json_encode($ouput);
	}
}

/* End of file listcustomers_model.php */
/* Location: ./application/models/listcustomers_model.php */